package com.leenmvc.core.dao.mysql.wrapper;

import com.leenmvc.core.base.BaseEntity;
import com.leenmvc.core.dao.communal.Wrapper;

import java.util.List;
import java.util.Map;

public class DeleteWrapper extends Wrapper {

    public DeleteWrapper() {}

    public DeleteWrapper(Class<?> cls) {
        this.cls = cls;
        String tableName = BaseEntity.getTableName(cls);
        this.sql.append("delete from ").append(tableName);
    }

    @Override
    public Wrapper addSet(String setKey, Object value) {
        return null;
    }

    @Override
    public String getSql() {
        this.sql.setLength(0);
        if(sql.indexOf("delete from ") == -1) {
            sql.append("delete from " + BaseEntity.getTableName(cls));
        }
//        sql.append(" "+ simpleName);
        if(where.length() > 0) {
            sql.append(" where " + where);
        }
        if(sql.indexOf(simpleName+".") != -1){
            String s = new String(sql);
            String replace = s.replaceAll(simpleName + ".", " ");
            sql = new StringBuilder(replace);
        }
        return this.sql.toString();
    }
    @Override
    public DeleteWrapper and() {
        this.where.append(" and ");
        return this;
    }

    @Override
    public Wrapper joinField(String field) {
        return null;
    }

    @Override
    public Wrapper joinOne(String field) {
        return this;
    }

    @Override
    public Wrapper joinMul(String field) {
        return this;
    }

    public DeleteWrapper and(String field, Object value) {
        this.where.append(" and ").append(field + " = ?");
        paramList.add(value);
        conditons.add(BaseEntity.humpLine(field) + " = ?");
        return this;
    }

    // or 连接符
    public DeleteWrapper or() {
        this.where.append(" or ");
        return this;
    }

    // 使用
    public DeleteWrapper linkOr() {
        int index = this.where.lastIndexOf(conditons.get(conditons.size() - 1), this.where.length() - 1);
        this.where.insert(index, "(");
        this.where.append(" or)");
        return this;
    }

    // 如果传参数那么默认就是用等号连接
    public DeleteWrapper or(String field, Object value) {
        this.where.append(" or ").append(field + " = ?");
        paramList.add(value);
        conditons.add(BaseEntity.humpLine(field) + " = ?");
        return this;
    }

    public DeleteWrapper count(Class<?> cls) {
        this.cls = cls;
        this.sql.append("select count(1) from ").append(BaseEntity.getTableName(cls));
        return this;
    }

    // order by
    public DeleteWrapper orderBy(String field, String desc) {
        this.orderBy.append(field).append(" ").append(desc);
        return this;
    }

    /**
     * 排除某个字段
     *
     * @param fields
     * @return
     */
    public DeleteWrapper decField(String... fields) {
        setAnd();
        for(String field: fields) {
            field = BaseEntity.humpLine(field);
            this.columns.remove(field);
        }
        return this;
    }

    /**
     * 只显示某些字段
     *
     * @param fields
     * @return
     */
    public DeleteWrapper incField(String... fields) {
        setAnd();
        this.columns.clear();
        for(String field: fields) {
            field = BaseEntity.humpLine(field);
            this.columns.add(field);
        }
        return this;
    }

    /**
     * 限制条数
     *
     * @param pageInfo
     * @return
     */
    public DeleteWrapper limit(Integer... pageInfo) {
        return this;
    }
    /**
     * 等于
     */
    public DeleteWrapper eq(String field, Object value) {
        super.eq(field, value);
        return this;
    }

    /**
     * 不等于
     *
     * @param field
     * @param value
     * @return
     */
    public DeleteWrapper uEq(String field, Object value) {
        super.uEq(field, value);
        return this;
    }

    /**
     * 右边随意
     *
     * @param field
     * @param value
     * @return
     */
    public DeleteWrapper likeR(String field, Object value) {
        if(value == null) return this;
        super.likeR(field, value);
        return this;
    }

    /**
     * 左边随意
     *
     * @param field
     * @param value
     * @return
     */
    public DeleteWrapper likeL(String field, Object value) {
        if(value == null) return this;
        super.likeL(field, value);
        return this;
    }

    /**
     * 全like
     *
     * @param field
     * @param value
     * @return
     */
    public DeleteWrapper like(String field, Object value) {
        super.like(field, value);
        return this;
    }

    /**
     * 根据数组中
     *
     * @param field
     * @param ins
     * @return
     */
    public DeleteWrapper in(String field, Object[] ins) {
        super.in(field, ins);
        return this;
    }

    @Override
    public <T> DeleteWrapper in(String field, List<T> ins) {
        return in(field, ins.toArray());
    }

    // between
    public DeleteWrapper between(String field, Object start, Object end) {
        super.between(field, start, end);
        return this;
    }

    // <
    public DeleteWrapper lt(String field, Object value) {
        super.lt(field, value);
        return this;
    }

    // >
    public DeleteWrapper gt(String field, Object value) {
        super.gt(field, value);
        return this;
    }

    // >=
    public DeleteWrapper ge(String field, Object value) {
        super.ge(field, value);
        return this;
    }

    // <=
    public DeleteWrapper le(String field, Object value) {
        super.le(field, value);
        return this;
    }

    // is null
    public DeleteWrapper isNull(String field) {
        super.isNull(field);
        return this;
    }

    // is not null
    public DeleteWrapper isNotNull(String field) {
        super.isNotNull(field);
        return this;
    }

    /**
     * 将hashMap转成QueryWrapper
     * like关键字 规则如下（前缀）：
     *     LL_  左像（%在左边）
     *     RL_  右像 (%在右边)
     *     AL_  全像 (%在两边)
     * orderBY关键字规则如下 （前缀）：
     *     OB_  排序的字段 value值为DESC 或 ESC
     *  page, pageSize 特殊字保留字 （只做分页功能）
     *
     *
     * @return
     */
    public DeleteWrapper mapToWrapper(Map<String, Object> params) {
        super.mapToWrapper(params);
        return this;
    }

    @Override
    public void copy(Wrapper wrapper)  {
        super.copy(wrapper);
    }
}
